//
//  ProjectCtrl.m
//  LaoWu
//
//  Created by yinqy on 15/4/23.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "ProjectCtrl.h"
#import "ProjectCell.h"
#import "ProjectHeaderView.h"
#import "ProjectDataTool.h"
#import "ProjectDetailCtrl.h"

@interface ProjectCtrl ()<UICollectionViewDataSource,UICollectionViewDelegate,UITextFieldDelegate>
@property (nonatomic,weak)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *wholeInfoArray;
@property(nonatomic,strong)ProjectHeaderView *headerView;
@property(nonatomic,assign)NSInteger currentTag;
@end
static NSString * const reuseIdentifier = @"ProjectCell";
@implementation ProjectCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    self.wholeInfoArray = [NSMutableArray array];
    self.navigationItem.title = @"工友通";
    [self initHeaderView];
    [self initCollectionView];
    [self.collectionView headerBeginRefreshing];
}

-(void)headerRefresh{
    [self getDataWithMaxId:0 failure:_currentTag keywords:_headerView.searchView.text];
}

-(void)footerRefresh{
    NSInteger maxId = 0;
    if (self.wholeInfoArray.count) {
        WorkReportItemResult *item = [self.wholeInfoArray lastObject];
        maxId = item.id;
    }
    if(maxId > 0){
        maxId =  maxId-1;
    }
    [self getDataWithMaxId:maxId failure:_currentTag keywords:_headerView.searchView.text];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    if(ISBLANK(_headerView.searchView.text)){
       [self getDataWithMaxId:0 failure:_currentTag keywords:_headerView.searchView.text];
    }else{
        [self getDataWithMaxId:0 failure:_currentTag keywords:_headerView.searchView.text];
        NSLog(@"搜索:%@",_headerView.searchView.text);
    }
    return YES;
}


-(void)initHeaderView{
    _headerView =[[UINib nibWithNibName:@"ProjectHeaderView" bundle:[NSBundle mainBundle]] instantiateWithOwner:self options:nil][0];
    [_headerView setFrame:CGRectMake(0, 0, SCREENW, 100)];
    __block ProjectCtrl *controller = self;
    [self.headerView.wholeBtn setSelected:YES];
    self.currentTag = self.headerView.wholeBtn.tag;
    _headerView.searchView.delegate = self;
    [self.headerView setTapProjectAction:^(NSInteger tag) {
        switch (tag) {
            case 0:
                NSLog(@"全部");
                 controller.currentTag = controller.headerView.wholeBtn.tag;
                [controller.headerView.wholeBtn setSelected:YES];
                [controller.headerView.signBtn setSelected:NO];
                [controller.headerView.notSignBtn setSelected:NO];
                [controller.headerView.qualityBtn setSelected:NO];
                [controller.collectionView headerBeginRefreshing];
                break;
            case 1:
                NSLog(@"未签约项目");
                controller.currentTag = controller.headerView.notSignBtn.tag;
                [controller.headerView.wholeBtn setSelected:NO];
                [controller.headerView.signBtn setSelected:NO];
                [controller.headerView.notSignBtn setSelected:YES];
                [controller.headerView.qualityBtn setSelected:NO];
                [controller.collectionView headerBeginRefreshing];
              
                break;
            case 2:
                 NSLog(@"已签约项目");
                controller.currentTag = controller.headerView.signBtn.tag;
                [controller.headerView.wholeBtn setSelected:NO];
                [controller.headerView.signBtn setSelected:YES];
                [controller.headerView.notSignBtn setSelected:NO];
                [controller.headerView.qualityBtn setSelected:NO];
                [controller.collectionView headerBeginRefreshing];
                break;
            case 3:
                NSLog(@"优质项目");
                controller.currentTag = controller.headerView.qualityBtn.tag;
                [controller.headerView.wholeBtn setSelected:NO];
                [controller.headerView.signBtn setSelected:NO];
                [controller.headerView.notSignBtn setSelected:NO];
                [controller.headerView.qualityBtn setSelected:YES];
                [controller.collectionView headerBeginRefreshing];
                break;
            default:
                break;
        }
    }];
    [self.view addSubview:_headerView];
}

-(void)getDataWithMaxId:(NSInteger)maxId failure:(NSInteger)failure keywords:(NSString*)keywords{

    [ProjectDataTool getProjectInfoData:maxId feature:failure  keywords:keywords success:^(ProjectInfoRes *result) {
        [self endRefresh];
        if(result.isSucc){
            if(maxId == 0){
                [self.wholeInfoArray removeAllObjects];
            }
            [self.wholeInfoArray addObjectsFromArray:result.datas];
            [self.collectionView reloadData];
        }else{
            [DialogUtils showTipsWithView:self.view String:result.msg];
        }
    } failure:^(NSError *error) {
        [self endRefresh];
        [DialogUtils showTipsWithView:self.view String:@"加载失败"];
    }];
}

-(void)endRefresh{
    [self.collectionView headerEndRefreshing];
    [self.collectionView footerEndRefreshing];
}

-(void)initCollectionView{
    UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc]init];
    [flowLayout setMinimumLineSpacing:5];
    [flowLayout setMinimumInteritemSpacing:5];
    UICollectionView *collectionView  =[[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    [collectionView setFrame:CGRectMake(0, CGRectGetMaxY(self.headerView.frame), SCREENW, collectionView.height-49 - 64 - CGRectGetMaxY(_headerView.frame))];
    [collectionView setBackgroundColor:RGBColor(240, 240, 240)];
    [collectionView registerClass:[ProjectCell class] forCellWithReuseIdentifier:@"collectionView"];
 
    [collectionView setDelegate:self];
    [collectionView setDataSource:self];
    [collectionView setAlwaysBounceVertical:YES];
    [collectionView registerNib:[UINib nibWithNibName:reuseIdentifier bundle: [NSBundle mainBundle]] forCellWithReuseIdentifier:reuseIdentifier];
    [collectionView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [collectionView addFooterWithTarget:self action:@selector(footerRefresh)];
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.wholeInfoArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ProjectCell *cell = [ProjectCell cellWithCollectionView:collectionView indexPath:indexPath];
    [cell initData:self.wholeInfoArray[indexPath.row]];
    [cell setBackgroundColor:[UIColor whiteColor]];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCREENW-15)/2, 200);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self.navigationController pushViewController:[ProjectDetailCtrl initWithProjectInfo:self.wholeInfoArray[indexPath.row]] animated:YES];
    NSLog(@"indexpath.row=%ld",indexPath.row);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5,5, 5);
}


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [self.view endEditing:YES];
}

@end
